package com.bleem.ds.xinshou.a01;

/**
 * @author SuYanchun
 * @since 2024-08-06 下午4:31
 */
public class Sort {

    public static void printArray(int[] arr) {
        for (int j : arr) {
            System.out.print(j+" ");
        }
        System.out.println();
    }

    public static void swap(int[] arr, int i, int j) {
        if (i == j) {
            return;
        }
        arr[i] += arr[j];
        arr[j] = arr[i] - arr[j];
        arr[i] -= arr[j];
    }

    public static void insertionSort(int[] arr) {
        if (null == arr || arr.length < 2) {
            return;
        }
        for (int i = 1; i < arr.length; i++) {
            int newValueIndex = i;
            while (newValueIndex > 0 && arr[newValueIndex - 1] > arr[newValueIndex]) {
                swap(arr, newValueIndex - 1, newValueIndex);
                newValueIndex--;
            }
        }

    }

    public static void selectSort(int[] arr) {
        if (null == arr || arr.length < 2) {
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            int minValueIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                minValueIndex = arr[j] < arr[minValueIndex] ? j : minValueIndex;
            }
            swap(arr, i, minValueIndex);
        }
    }

    public static void bubbleSort(int[] arr){
        if (null == arr || arr.length < 2) {
            return;
        }
        for (int i = arr.length - 1; i >= 0; i--) {
            for (int j = 0; j < i; j++) {
                if (arr[j] > arr[j + 1]) {
                    swap(arr, j, j + 1);
                }
            }
        }
    }


    public static void main(String[] args) {
        int[] arr = {99, 2, 6, 14, 1, 87};
        bubbleSort(arr);
        printArray(arr);
    }

}
